package com.cdu.xinmiao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cdu.xinmiao.R;
import com.cdu.xinmiao.Utils.PopupUtils;
import com.cdu.xinmiao.adapter.BannerAdapter;
import com.cdu.xinmiao.adapter.TaskAdapter;
import com.cdu.xinmiao.data.Task;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xuexiang.xui.adapter.simple.XUISimpleAdapter;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.banner.recycler.BannerLayout;
import com.xuexiang.xui.widget.popupwindow.easypopup.ListPopup;
import com.xuexiang.xui.widget.popupwindow.popup.XUIListPopup;
import com.xuexiang.xui.widget.popupwindow.popup.XUIPopup;
import com.xuexiang.xui.widget.textview.MarqueeTextView;
import com.xuexiang.xui.widget.textview.marqueen.DisplayEntity;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private MarqueeTextView tv_notice;
    private TitleBar titleBar;
    private RecyclerView recyclerView;
    private BannerLayout bannerLayout;
    private BannerAdapter mAdapter;
    private List<Integer> mImageList;
    private SuperButton switch_mode;
    private SuperButton show_activity;
    private PopupUtils listPopup;
    private BottomNavigationView bottomNavigationView;
    private boolean isMode1 = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_home);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.home), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        getId();

        loadData();

        loadImageData();

        initAdapter();

        bindClick();
    }

    private void getId() {
        tv_notice = findViewById(R.id.tv_notice);
        titleBar = findViewById(R.id.titleBar_back);
        bannerLayout = findViewById(R.id.banner_layout);
        switch_mode = findViewById(R.id.switch_mode);
        show_activity = findViewById(R.id.show_activity);
        recyclerView = findViewById(R.id.task_recycler_view);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setSelectedItemId(R.id.item_home);

        listPopup = new PopupUtils(this);
    }

    private void initAdapter() {
        mAdapter = new BannerAdapter(mImageList);
        bannerLayout.setAdapter(mAdapter);
    }

    private void loadImageData() {
        // 准备轮播图数据
        mImageList = new ArrayList<>();
        mImageList.add(R.drawable.img1);
        mImageList.add(R.drawable.img2);
        mImageList.add(R.drawable.img3);
    }

    private void loadData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        String subTitleText = isMode1 ? "快去完成你的任务吧！" : "距离开学还有 5 天！";
        titleBar.setSubTitle(subTitleText);
        List<Task> taskList = isMode1 ? getTaskListMode1() : getTaskListMode2();
        TaskAdapter adapter = new TaskAdapter(taskList);
        recyclerView.setAdapter(adapter);
    }

    private List<Task> getTaskListMode1() {
        List<Task> taskList = new ArrayList<>();
        taskList.add(new Task("去十教打卡", false));
        taskList.add(new Task("去六食堂打卡", true));
        taskList.add(new Task("去综合楼的樱花林打卡", false));
        taskList.add(new Task("去乐健山打卡", false));

        // 未完成任务靠前
        taskList.sort(new Comparator<Task>() {
            @Override
            public int compare(Task task1, Task task2) {
                if (task1.isComplete() && !task2.isComplete()) {
                    return 1;
                } else if (!task1.isComplete() && task2.isComplete()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        return taskList;
    }

    private List<Task> getTaskListMode2() {
        List<Task> taskList = new ArrayList<>();
        taskList.add(new Task("准备录取通知书", false));
        taskList.add(new Task("准备银行卡", true));
        taskList.add(new Task("准备衣服", false));
        taskList.add(new Task("准备学习用品", false));

        // 未完成任务靠前
        taskList.sort(new Comparator<Task>() {
            @Override
            public int compare(Task task1, Task task2) {
                if (task1.isComplete() && !task2.isComplete()) {
                    return 1;
                } else if (!task1.isComplete() && task2.isComplete()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        return taskList;
    }

    private void bindClick() {
        //返回上个页面
        titleBar.setLeftClickListener(v -> finish());
        switch_mode.setOnClickListener(v -> switchMode());
        show_activity.setOnClickListener(v -> showActivity());

        List<String> datas = new ArrayList<String>();
        datas.add("成大欢迎你！");
        datas.add("快去完成任务吧！");
        datas.add("校训: 自信 自强 自尊 自爱");
        tv_notice.setOnMarqueeListener(new MarqueeTextView.OnMarqueeListener() {
            @Override
            public DisplayEntity onStartMarquee(DisplayEntity displayMsg, int index) {
                if (displayMsg.toString().isEmpty()) {
                    return null;
                } else {
                    //XToastUtils.toast("开始滚动：" + displayMsg.toString());
                    return displayMsg;
                }
            }

            @Override
            public List<DisplayEntity> onMarqueeFinished(List<DisplayEntity> displayDatas) {
                //XToastUtils.toast("一轮滚动完毕！");
                return displayDatas;
            }
        });
        tv_notice.startSimpleRoll(datas);

        // 设置底部导航栏监视器
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item_home:
                        break;
                    case R.id.item_user:
                        Intent intent2 = new Intent(HomeActivity.this, UserActivity.class);
                        startActivity(intent2);
                        break;
                    case R.id.item_mission:
                        Intent intent3 = new Intent(HomeActivity.this, MissionActivity.class);
                        startActivity(intent3);
                        break;
                    case R.id.item_rank:
                        Intent intent4 = new Intent(HomeActivity.this, RankActivity.class);
                        startActivity(intent4);
                        break;

                }
                return false;
            }
        });

        findViewById(R.id.show_tools).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 向下显示Popup
                listPopup.showDown(v);
            }
        });
    }

    private void showActivity() {
        Intent intent = new Intent(HomeActivity.this, ShowActivity.class);
        startActivity(intent);
    }

    private void switchMode() {
        // 切换模式
        isMode1 = !isMode1;
        // 重新加载数据并更新RecyclerView
        loadData();
    }
}